package com.blb.servlet.user;

import com.blb.pojo.*;
import com.blb.service.user.impl.IndexServiceImpl;
import com.blb.servlet.BaseServlet;
import com.blb.utils.ApplicationContextUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(value = "/user/index/*", loadOnStartup = 1)
public class IndexServlet extends BaseServlet {

    private IndexServiceImpl indexService = (IndexServiceImpl) ApplicationContextUtil.getBean(IndexServiceImpl.class);

    // 进入购物车
    public void shoppingCart(HttpServletRequest req, HttpServletResponse resp) {
        try {
            System.out.println(123);
            forward("/user/view/index/shoppingCart");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 得到订单数据从商品详细中
    public void getOrdersData(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        User user = (User) req.getSession().getAttribute("user");
        String productId = req.getParameter("productId");
        String num = req.getParameter("num");

        List<Product> pAndBList = indexService.getProductAndBrand(String.valueOf(user.getId()));
        indexService.setOrderData((int) user.getId(), Integer.parseInt(productId), Integer.parseInt(num));
        req.setAttribute("pAndBList", pAndBList);
    }

    // 进入商品详细页面

    public void goodsDetail(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        try {
            String id = req.getParameter("id");
            // 商品分类
            List<ProductType> productTypes = indexService.getProductType();
            // 查询所以后的product
            List<Product> products = indexService.selectProduct();
            List<Product> pAndBList = indexService.getProductAndBrand(id);
            req.setAttribute("pAndBList", pAndBList);
            req.setAttribute("products",products);
            req.setAttribute("productTypes", productTypes);
            forward("/user/view/index/goodsDetail");
        } catch (Exception e) {
            e.printStackTrace();
            forward("/WEB-INF/jsp/500.jsp");
        }
    }

    public void index(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        try {
            // 查询所以后的product
            List<Product> products = indexService.selectProduct();
            // 轮播图左边的导航栏
            List<Carousel> Carousels = indexService.getCarousel();
            // 新品
            List<Product> newProducts = indexService.getNewProduct();
            // 商品分类
            List<ProductType> productTypes = indexService.getProductType();
            //查询分类表，设置显示条数
            List<Product> ProductAndBrand = indexService.getProductAndBrand("当前不需要id");

            req.setAttribute("products",products);
            req.setAttribute("carousels", Carousels);
            req.setAttribute("newProduct", newProducts);
            req.setAttribute("productTypes", productTypes);
            req.setAttribute("productAndBrand", ProductAndBrand);

            forward("/user/view/index");
        } catch (Exception e) {
            e.printStackTrace();
            forward("/WEB-INF/jsp/500.jsp");
        }
    }


    public void buyerTrade(HttpServletRequest req, HttpServletResponse resp) {
        try {
            forward("/user/view/index/buyerTrade");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
